package com.best.oasis.settlement.common.entity.security;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

import com.best.oasis.settlement.common.entity.base.BaseBizEntity;

/**
 * It implements Acegi Security's UserDetails interface. 用户
 */
@Entity
@Table(name = "ST_USER")
public class User extends BaseBizEntity implements Serializable, UserDetails {
    private static final long serialVersionUID = 3832626162173359411L;

    public User() {
    }

    public User(final String username) {
        this.userName = username;
    }

    /**
     * 用户登录名
     */
    @Column(nullable = false, length = 50, unique = true,name="userName")
    private String  userName;
    /**
     * 用户名称
     */
    @Column(name = "name", nullable = false, length = 50)
    private String  name;
    /**
     * 密码
     */
    @Column(nullable = false,name="password")
    private String  password;
    /**
     * 密码重置的key
     */
    private String  passwordResetKey;

    //	@ManyToMany(cascade=CascadeType.REFRESH, fetch = FetchType.LAZY)
    //	@JoinTable(name = "ST_USER_ROLE", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = @JoinColumn(name = "role_id"))
    //	private List<Role> roles = new ArrayList<Role>();
    //
    //	@ManyToMany(cascade=CascadeType.REFRESH, fetch = FetchType.LAZY)
    //	@JoinTable(name = "ST_USER_ROLEGROUP", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = @JoinColumn(name = "group_id"))
    //	private List<RoleGroup> roleGroups = new ArrayList<RoleGroup>();
    /**
     * 当前用户是否启用
     */
    private boolean enabled;

    private boolean accountExpired;

    private boolean accountLocked;

    private boolean credentialsExpired;
    /**
     * 省
     */
    private String  province;
    /**
     * 城市
     */
    private String  city;
    /**
     * 乡镇
     */
    private String  town;
    /**
     * 街道
     */
    private String  street;
    /**
     * 邮编
     */
    private String  postCode;
    /**
     * 电话区号
     */
    private String  telephoneAreaCode;
    /**
     * 电话号码
     */
    private String  telephoneNumber;
    /**
     * 分机号
     */
    private String  telephoneExtention;
    /**
     * 部门
     */
    private String  department;
    /**
     * 职位
     */
    private String  jobTitle;
    /**
     * email
     */
    private String  email;
    /**
     * 手机号码
     */
    private String  mobile;

    private boolean ldapUser;          //是否Ldap用户

    private List<Role> roles;
    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getPostCode() {
        return postCode;
    }

    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    public String getTelephoneAreaCode() {
        return telephoneAreaCode;
    }

    public void setTelephoneAreaCode(String telephoneAreaCode) {
        this.telephoneAreaCode = telephoneAreaCode;
    }

    public String getTelephoneNumber() {
        return telephoneNumber;
    }

    public void setTelephoneNumber(String telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }

    public String getTelephoneExtention() {
        return telephoneExtention;
    }

    public void setTelephoneExtention(String telephoneExtention) {
        this.telephoneExtention = telephoneExtention;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPassword() {
        return password;
    }

    public String getPasswordResetKey() {
        return passwordResetKey;
    }

    public void setPasswordResetKey(String passwordResetKey) {
        this.passwordResetKey = passwordResetKey;
    }

    public boolean isLdapUser() {
        return ldapUser;
    }

    public void setLdapUser(boolean ldapUser) {
        this.ldapUser = ldapUser;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
     * @return GrantedAuthority[] an array of roles.
     */
    @Transient
    public GrantedAuthority[] getAuthorities() {
        return roles.toArray(new GrantedAuthority[0]);
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isAccountExpired() {
        return accountExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired()
     */
    @Transient
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }

    public boolean isAccountLocked() {
        return accountLocked;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
     */
    @Transient
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }

    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired()
     */
    @Transient
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }

    public void setAccountLocked(boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public void setCredentialsExpired(boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }

        final User user = (User) o;

        return !(userName != null ? !userName.equals(user.getUsername())
                : user.getUsername() != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (userName != null ? userName.hashCode() : 0);
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}
